/**
 * Created by Administrator on 2016/12/21 0021.
 */
$.fn.extend({
    calendar:function(opt){
        $(this).html('');
        var that = $(this);
        var table = $("<div />").addClass("calendar-grid");
        var  paddy =  function (n, p, c) {
            var pad_char = typeof c !== 'undefined' ? c : '0';
            var pad = new Array(1 + p).join(pad_char);
            return (pad + n).slice(-pad.length);
        };
        var selectedDays = opt.selectedDays ?  opt.selectedDays.split(','): [];
        var  dateFromNumbers = function(year,month,day){
            return new Date(paddy(year, 4) + "/" +  paddy(month, 2) + "/" + paddy(day, 2));
        };
        var contains = function (a, d) {
            var i = a.length;
            while (i--) {
                if (a[i] == d) {
                    return true;
                }
            }
            return false;
        }
        var feb = 28;
        if (opt.month === 1) {
            if ((opt.year % 100 !== 0) && (opt.year % 4 === 0) || (opt.year % 400 === 0)) {
                feb = 29;
            }
        }
        var totalDays = ["31", "" + feb + "", "31", "30", "31", "30", "31", "31", "30", "31", "30", "31"];
        var daysInMonth = totalDays[opt.month-1];
        var first_week_day = dateFromNumbers(opt.year, opt.month, 1).getDay();
        var  weekDays = ["周日","周一","周二","周三","周四","周五","周六"];
        var tr = $("<div />").addClass("calendar-row week-days");
        for(var i = 0; i < 7; i++){
            var td =$("<div/>").addClass("calendar-cell align-center day-of-week");
            td.appendTo(tr);
            td.html( ""+weekDays[i]+"");
        }
        tr.appendTo(table);
        tr = $("<div />").addClass("calendar-row");
        tr.appendTo(table);
        // 添加前面的空白
        if(first_week_day > 0){
            for(var i = 0; i < first_week_day; i++){
                var td =$("<div/>").addClass("calendar-cell align-center");
                td.appendTo(tr);
                td.html( "<div>&nbsp;</div>");
            }
        }
        for(var i = 1; i <= daysInMonth; i++){
            var td =$("<div/>").addClass("calendar-cell align-center");
            if(contains(selectedDays,i)){
                td.addClass("selected");
            }
            td.html( "<div><a href='#'>" + i +"</a></div>");
            td.appendTo(tr);
            var currentWeek =  dateFromNumbers(opt.year, opt.month, i).getDay();
            if( currentWeek === 6 && i !== daysInMonth ){
                tr.appendTo(table);
                tr = $("<div />").addClass("calendar-row");
            }
        }
        tr.appendTo(table);
        table.find("a").on("click",function(e){
            var $t = $(this);
            if($t.parent().parent().hasClass("selected")){
                $t.parent().parent().removeClass("selected");
            }else{
                $t.parent().parent().addClass("selected");
            }
        });
        table.appendTo(that);
    },

    getDays:function(){
        var that = $(this);
        var selectedDay = that.find(".selected").find("a");
        var days = [];
        $.each(selectedDay,function(i,d){
            //console.log(d);
            days.push($(d).html());
        });
        //console.log(days);
        return days.join(",");
    }
});